﻿#include "CVideoDevCfgOper.h"
#include "Header.h"
#include "CSqlOper.h"
#include "Public.h"
#include "CGlobal.h"

CVideoDevCfgOper::CVideoDevCfgOper(CDevDataCam* pDevData,QWidget *parent)
	: m_pDevData(pDevData), QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("摄像头参数配置"));
	ui.lineEditServerIp->setText(g_sAIServerIp);
	ui.lineEditRecordPath->setText(g_sResultPath);
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	setStyleSheetFile();
	initConnect();
	ui.comboBoxVideoType->addItem(A2T("单目摄像头"));
	ui.comboBoxVideoType->addItem(A2T("双目摄像头"));
	setData();
	m_nType = 1;
}

CVideoDevCfgOper::CVideoDevCfgOper(QString sDevGuid,QWidget *parent /*= nullptr*/)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("摄像头参数配置"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	setStyleSheetFile();
	ui.lineEditServerIp->setText(g_sAIServerIp);
	ui.lineEditRecordPath->setText(g_sResultPath);
	m_sDevGuid = sDevGuid;
	initConnect();
	ui.comboBoxVideoType->addItem(A2T("单目摄像头"));
	ui.comboBoxVideoType->addItem(A2T("双目摄像头"));
	m_nType = 0;
}

CVideoDevCfgOper::~CVideoDevCfgOper()
{}

void CVideoDevCfgOper::initConnect()
{
	connect(ui.pushButtonOK, &QPushButton::clicked, this, &CVideoDevCfgOper::slotPushBtnOK);
	connect(ui.pushButtonCancle, &QPushButton::clicked, this, &CVideoDevCfgOper::slotPushBtnCancle);
	connect(ui.lineEditCamerIP, &QLineEdit::textChanged, this, &CVideoDevCfgOper::slotCamerIpChanged);
	connect(ui.comboBoxVideoType, QOverload<const QString &>::of(&QComboBox::currentIndexChanged), this, &CVideoDevCfgOper::slotVideoTypeChanged);
}

void CVideoDevCfgOper::setData()
{
	if (m_pDevData == nullptr)
	{
		return;
	}
	ui.lineEditCamerName->setText(m_pDevData->m_sCamerName);
	ui.lineEditServerIp->setText(m_pDevData->m_sServerIp);
	ui.lineEditCamerIP->setText(m_pDevData->m_sCamerIp);
	ui.lineEditCamerPort->setText(QString::number(m_pDevData->m_nCamerPort));
	ui.lineEditCamerUser->setText(m_pDevData->m_sUserName);
	ui.lineEditCamerPasswd->setText(m_pDevData->m_sPassWord);
	ui.lineEditRecordPath->setText(m_pDevData->m_sRecordPath);
	ui.lineEditRtspUrl->setText(m_pDevData->m_sRtspUrlImg);
	ui.lineEditChannel->setText(QString::number(m_pDevData->m_nChannel));
	ui.lineEditRtspUrlHW->setText(m_pDevData->m_sRtspUrlIr);
	ui.lineEditChannelHW->setText(QString::number(m_pDevData->m_nChannelHW));
	if (m_pDevData->m_nVideoType == 0)
	{
		ui.comboBoxVideoType->setCurrentText(A2T("单目摄像头"));
	}
	else if (m_pDevData->m_nVideoType == 1)
	{
		ui.comboBoxVideoType->setCurrentText(A2T("双目摄像头"));
	}
	for (int i = 0; i < m_pDevData->m_vecAlgType.size(); i++)
	{
		int nType = m_pDevData->m_vecAlgType[i];
		switch (nType)
		{
		case EAlgType::enCoalFlow:
			ui.checkBoxCoalFlow->setChecked(true);
			break;
		case EAlgType::enBeltDeviation:
			ui.checkBoxBeltDeviation->setChecked(true);
			break;
		case EAlgType::enBeltForeign:
			ui.checkBoxBeltForeign->setChecked(true);
			break;
		case EAlgType::enLargeCoal:
			ui.checkBoxLargeCoal->setChecked(true);
			break;
		case EAlgType::enSafetyHelmet:
			ui.checkBoxSafetyHelmet->setChecked(true);
			break;
		case EAlgType::enSmokeFire:
			ui.checkBoxSmokeFire->setChecked(true);
			break;
		case EAlgType::enSmokeMakeCall:
			ui.checkBoxSmokeMakeCall->setChecked(true);
			break;
		case EAlgType::enTemp:
			ui.checkBoxTemp->setChecked(true);
			break;
		case EAlgType::enPerimeter:
			ui.checkBoxPerimeter->setChecked(true);
			break;
		case EAlgType::enBlock:
			ui.checkBoxBlock->setChecked(true);
			break;
		case EAlgType::enThrow:
			ui.checkBoxThrow->setChecked(true);
			break;
		case EAlgType::enStreet:
			ui.checkBoxStreet->setChecked(true);
			break;
		default:
			break;
		}
	}
}

void CVideoDevCfgOper::setStyleSheetFile()
{
	
}

void CVideoDevCfgOper::slotPushBtnOK()
{
	QString sCamerName = ui.lineEditCamerName->text();
	QString sCamerIp = ui.lineEditCamerIP->text();
	int nCamerPort = ui.lineEditCamerPort->text().toInt();
	QString sCamerUser = ui.lineEditCamerUser->text();
	QString sCamerPasswd = ui.lineEditCamerPasswd->text();
	QString sRecordPath = ui.lineEditRecordPath->text();
	QString sRtspUrl = ui.lineEditRtspUrl->text();
	QString sRtspUrlHW = ui.lineEditRtspUrlHW->text();
	int nChannel = ui.lineEditChannel->text().toInt();
	int nChannelHW = ui.lineEditChannelHW->text().toInt();
	QString sServerIp = ui.lineEditServerIp->text();
	int nVideoType = 0;
	if (ui.comboBoxVideoType->currentText() == A2T("单目摄像头"))
	{
		nVideoType = 0;
		sRtspUrlHW = "";
		nChannelHW = -1;
	}
	else if (ui.comboBoxVideoType->currentText() == A2T("双目摄像头"))
	{
		nVideoType = 1;
	}
	//首先验证数据有效性
	if (sCamerIp == "" || sCamerName == "" || sCamerUser == "" || sCamerPasswd == "" || sRecordPath == "" || sRtspUrl == "" 
		|| ui.lineEditCamerPort->text() == "" || ui.lineEditChannel->text() == "" || sServerIp=="")
	{
		messageBox(1, A2T("请将数据填写完整后再进行操作"));
		return;
	}
	//判断服务器IP和通道号是否正确
	{
		QString sErrMsg = "";
		QVector<QSqlRecord> vecInfo;
		CSqlOper::instance().selectServerChannel(sServerIp, nChannel, vecInfo, sErrMsg);
		if (vecInfo.size() > 0)
		{
			messageBox(1, A2T("数据库中已经存在算法服务器为:%1 通道号为:%2").arg(sServerIp).arg(nChannel));
			return;
		}
	}
	QVector<int> vecAlgType;
	if (ui.checkBoxBeltDeviation->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enBeltDeviation);
	}
	if (ui.checkBoxBeltForeign->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enBeltForeign);
	}
	if (ui.checkBoxCoalFlow->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enCoalFlow);
	}
	if (ui.checkBoxLargeCoal->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enLargeCoal);
	}
	if (ui.checkBoxSafetyHelmet->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enSafetyHelmet);
	}
	if (ui.checkBoxSmokeFire->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enSmokeFire);
	}
	if (ui.checkBoxSmokeMakeCall->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enSmokeMakeCall);
	}
	if (ui.checkBoxTemp->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enTemp);
	}
	if (ui.checkBoxPerimeter->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enPerimeter);
	}
	if (ui.checkBoxBlock->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enBlock);
	}
	if (ui.checkBoxThrow->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enThrow);
	}
	if (ui.checkBoxStreet->checkState() == Qt::CheckState::Checked)
	{
		vecAlgType.push_back(EAlgType::enStreet);
	}
	QString sAlgType = "";
	for (int i = 0; i < vecAlgType.size(); i++)
	{
		sAlgType += QString::number(vecAlgType[i]);
		sAlgType += ",";
	}

	if (m_nType == 1)//修改
	{
		QString sErrMsg = "";
		bool bRet = CSqlOper::instance().updateCamerInfo(m_pDevData->m_sDevGuid, sCamerName, sCamerIp, nCamerPort, sCamerUser, sCamerPasswd, sRecordPath, sRtspUrl,
			sAlgType, nChannel,sServerIp,nVideoType,nChannelHW,sRtspUrlHW, sErrMsg);
		if (bRet == true)
		{
			//保存成功后要将自己的数据也要更新，
			m_pDevData->m_pMutex->lock();
			m_pDevData->m_sCamerIp = sCamerIp;
			m_pDevData->m_sCamerName = sCamerName;
			m_pDevData->m_sServerIp = sServerIp;
			m_pDevData->m_nCamerPort = nCamerPort;
			m_pDevData->m_nChannel = nChannel;
			m_pDevData->m_sUserName = sCamerUser;
			m_pDevData->m_sPassWord = sCamerPasswd;
			m_pDevData->m_sRecordPath = sRecordPath;
			m_pDevData->m_sRtspUrlImg = sRtspUrl;
			m_pDevData->m_vecAlgType= vecAlgType;
			m_pDevData->m_pMutex->unlock();
			messageBox(0, A2T("保存成功"));
			this->accept();
		}
		else
		{
			messageBox(1, A2T("保存失败,原因:%1").arg(sErrMsg));
		}
	}
	else if (m_nType == 0)//增加
	{
		QString sErrMsg = "";
		bool bRet = CSqlOper::instance().insertCamerInfo(m_sDevGuid, sCamerName, sCamerIp, nCamerPort, sCamerUser, sCamerPasswd, sRecordPath, sRtspUrl, sAlgType,
			nChannel,sServerIp, nVideoType, nChannelHW, sRtspUrlHW, sErrMsg);
		if (bRet == true)
		{
			messageBox(0, A2T("保存成功"));
			this->accept();
		}
		else
		{
			messageBox(1, A2T("保存失败,原因:%1").arg(sErrMsg));
		}
	}
	
}

void CVideoDevCfgOper::slotPushBtnCancle()
{
	this->close();
}

void CVideoDevCfgOper::slotCamerIpChanged(const QString &text)
{
	ui.lineEditRtspUrl->setText(QString("rtsp://admin:ky002380@%1:554/h264/ch1/main/av_strea").arg(text));
	ui.lineEditRtspUrlHW->setText(QString("rtsp://admin:ky002380@%1:554/h264/ch2/main/av_strea").arg(text));
}

void CVideoDevCfgOper::slotVideoTypeChanged(const QString &text)
{
	if (text == A2T("单目摄像头"))
	{
		ui.label_12->hide();
		ui.lineEditChannelHW->hide();
		ui.label_11->hide();
		ui.lineEditRtspUrlHW->hide();
	}
	else if (text == A2T("双目摄像头"))
	{
		ui.label_12->show();
		ui.lineEditChannelHW->show();
		ui.label_11->show();
		ui.lineEditRtspUrlHW->show();
	}
}
